package com.diamond.service.impl;

import java.util.List;

import com.diamond.bo.InlayDiaPicBO;
import com.diamond.bo.InlayDiamonBO;
import com.diamond.bo.QueryInlayDiamondBO;
import com.diamond.bo.ReturnBO;
import com.diamond.po.InlayDiamondPO;
import com.diamond.service.IInlayDiamondService;
import com.diamond.util.StringUtil;

/**
 * @author JSmart Tools
 */
@SuppressWarnings("unchecked")
public class InlayDiamondService extends Service<InlayDiamondPO> implements IInlayDiamondService {

	public InlayDiamonBO queryInlayDiamondDetail(String inlayDiaId)
			throws Exception {
		StringBuilder sql = new StringBuilder("select * from t_inlay_diamond tid, user u " +
				"where tid.user_id = u.user_id");
		if (!StringUtil.isEmpty(inlayDiaId)) {
			sql.append(" and tid.inlay_diamond_id=").append(inlayDiaId);
		}
		List<InlayDiamonBO> list = super.genericDAO.findBySqlToObj(sql.toString(), InlayDiamonBO.class);
		if (list.size() > 0) {
			return list.get(0);
		} else {
			return null;	
		}
	}

	public ReturnBO<InlayDiamonBO> queryInlayDiamondList(QueryInlayDiamondBO queryInlayDiamondBo) throws Exception {
		ReturnBO<InlayDiamonBO> returnBo = new ReturnBO<InlayDiamonBO>();
		StringBuilder sql = new StringBuilder("select * from t_inlay_diamond tid, user u where tid.user_id=u.user_id ");
		StringBuilder totalSql = new StringBuilder("select * from t_inlay_diamond tid, user u where tid.user_id=u.user_id ");
		if (!StringUtil.isEmpty(queryInlayDiamondBo.getInlayDiaCode())) {
			sql.append(" and tid.inlay_number like '%").append(queryInlayDiamondBo.getInlayDiaCode()).append("%'");
			totalSql.append(" and tid.inlay_number like '%").append(queryInlayDiamondBo.getInlayDiaCode()).append("%'");
		}
		if (!StringUtil.isEmpty(queryInlayDiamondBo.getInlayDiaName())) {
			sql.append(" and tid.inlay_name like '%").append(queryInlayDiamondBo.getInlayDiaName()).append("%'");
			totalSql.append(" and tid.inlay_name like '%").append(queryInlayDiamondBo.getInlayDiaName()).append("%'");
		}
		if (!StringUtil.isEmpty(queryInlayDiamondBo.getInlayDiaType())) {
			sql.append(" and tid.inlay_type = '").append(queryInlayDiamondBo.getInlayDiaType()).append("'");
			totalSql.append(" and tid.inlay_type = '").append(queryInlayDiamondBo.getInlayDiaType()).append("'");
		}
		if (!StringUtil.isEmpty(queryInlayDiamondBo.getInlayDiaPriFr()) && !StringUtil.isEmpty(queryInlayDiamondBo.getInlayDiaPriTo())) {
			sql.append(" and tid.k18_price < ").append(queryInlayDiamondBo.getInlayDiaPriTo()).append(" and tid.k18_price > ").append(queryInlayDiamondBo.getInlayDiaPriFr());
			totalSql.append(" and tid.k18_price < ").append(queryInlayDiamondBo.getInlayDiaPriTo()).append(" and tid.k18_price > ").append(queryInlayDiamondBo.getInlayDiaPriFr());
		}
		sql.append(" order by tid.k18_price").append(" limit ").append(queryInlayDiamondBo.getStart()).append(", ").append(queryInlayDiamondBo.getPageSize());;
		List<InlayDiamonBO> list = super.genericDAO.findBySqlToObj(sql.toString(), InlayDiamonBO.class);
		List totalSize = super.genericDAO.findBySqlToObj(totalSql.toString(), null);
		returnBo.setTotalSize(totalSize.size());
		returnBo.setReturnBolist(list);
		returnBo.setPageSize(queryInlayDiamondBo.getPageSize());
		returnBo.setStart(queryInlayDiamondBo.getStart());
		return returnBo;
	}

	public Boolean updateInlayDiamond(InlayDiamonBO inlayDiamondBo)
			throws Exception {
		
		return false;
	}

	public List<InlayDiaPicBO> queryInlayDiamondPic(String inlayDiaId)
			throws Exception {
		StringBuilder sql = new StringBuilder("select * from t_inlay_dia_pic tidp where 1=1 ");
		if (!StringUtil.isEmpty(inlayDiaId)) {
			sql.append(" and tidp.inlay_diamond_id=").append(inlayDiaId);
		}
		List<InlayDiaPicBO> list = super.genericDAO.findBySqlToObj(sql.toString(), InlayDiaPicBO.class);
		return list;
	}

	public void delInlayDiamond(String inlayDiaId) {
		String delSqlInlayDiamond = "delete from t_inlay_diamond where inlay_diamond_id=" + inlayDiaId;
		String delSqlInlayDiamondPic = "delete from t_inlay_dia_pic where inlay_diamond_id="+inlayDiaId;
		super.genericDAO.updateBySql(delSqlInlayDiamond);
		super.genericDAO.updateBySql(delSqlInlayDiamondPic);
	}
 

}
